package com.ant.rxjava.webtest.ui;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.ant.rxjava.webtest.R;
import com.ant.rxjava.webtest.app.MyApplication;
import com.ant.rxjava.webtest.databinding.AcMainLayoutBinding;
import com.ant.rxjava.webtest.ui.base.BaseActivity;
import com.ant.rxjava.webtest.utils.MyConstant;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private AcMainLayoutBinding binding;
    private String url = "";

    @Override
    public void initLayout() {
        binding = DataBindingUtil.setContentView(this, R.layout.ac_main_layout);
        initToolBar();
        setTitle("HTML");
    }

    @Override
    public void initView() {
        WebSettings settings = binding.acMainWeb.getSettings();
        settings.setAllowContentAccess(true);
        settings.setJavaScriptEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setDomStorageEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        binding.acMainWeb.addJavascriptInterface(new MyBrage(this), "android");
    }

    @Override
    public void initListener() {

        binding.acMainTempt.setOnClickListener(this);
        binding.acMainWeb.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String title = binding.acMainWeb.getTitle();
                if (!TextUtils.isEmpty(title)) {
                    setTitle(title);
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (view != null && !url.isEmpty()) {
                    if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ftp://"))
                        view.loadUrl(url);
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    @Override
    public void initData() {
        MyConstant constant = ((MyApplication) getApplication()).getConstant();
        url = constant.getUrl();
        if (TextUtils.isEmpty(url)) {
            binding.acMainTempt.setVisibility(View.VISIBLE);
        } else {
            binding.acMainTempt.setVisibility(View.GONE);
            binding.acMainWeb.loadUrl(url);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ac_main_tempt:
                startActivityForResult(new Intent(this, IndexActivity.class), REQUEST_URL);
                break;
        }
    }

    private long exitTime = 0l;

    @Override
    public void onBackPressed() {
        if (binding.acMainWeb.canGoBack()) {
            binding.acMainWeb.goBack();
        } else {
            if (System.currentTimeMillis() - exitTime > 1000) {
                exitTime = System.currentTimeMillis();
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            } else {
                super.onBackPressed();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.ac_main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.ac_main_menu_url:
                startActivityForResult(new Intent(this, IndexActivity.class), REQUEST_URL);
                return true;
            case R.id.ac_main_menu_refresh:
                binding.acMainWeb.reload();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static final int REQUEST_URL = 0x2342;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_URL:
                if (RESULT_OK == resultCode) {
                    url = ((MyApplication) getApplication()).getConstant().getUrl();
                    binding.acMainTempt.setVisibility(View.GONE);
                    binding.acMainWeb.loadUrl(url);
                }
                break;
        }
    }


    class MyBrage {

        private Context mContext;

        public MyBrage(Context context) {
            mContext = context;
        }

        @JavascriptInterface
        public void alert(String mess) {
            if (TextUtils.isEmpty(mess))
                return;
            Toast.makeText(mContext, mess, Toast.LENGTH_LONG).show();
        }

    }
}
